﻿using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace System.Collections
{
    public static class EnumerableExtensions
    {
        public static bool Is<TModel, TProperty>(this TModel model, Expression<Func<TModel, TProperty>> propertyAccessor)
        {
            return typeof(IEnumerable).IsAssignableFrom(typeof(TProperty));
        }
        public static T[] AsArray<T>(this IEnumerable<T> enumerable)
        {
            if (enumerable is T[])
            {
                return (T[])enumerable;
            }
            return enumerable.ToArray();
        }
        public static List<T> AsList<T>(this IEnumerable<T> enumerable)
        {
            if (enumerable is List<T>)
            {
                return (List<T>)enumerable;
            }
            return enumerable.ToList();
        }

        public static void ForEach<T>(this IEnumerable<T> enumerable, Action<T> action)
        {
            if (enumerable != null)
            {
                foreach (var value in enumerable)
                {
                    action(value);
                }
            }
        }

        public static void ForEach<T>(this IEnumerable<T> enumerable, Action<T[], int> action)
        {
            var index = 0;
            T[] array = enumerable.AsArray();
            enumerable.ForEach(value =>
            {
                action(array, index);
                index++;
            });
        }

        public static void ForEach<T>(this IEnumerable<T> enumerable, Action<int, T> action)
        {
            var index = 0;
             enumerable.ForEach(value =>
            {
                action(index, value);
                index++;
            });
        }
    }
}
